package com.freedy.concurrent.Monitors.reentrantLock;

import com.freedy.concurrent.Monitors.Sleeper;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Freedy
 * @date 2021/4/22 21:29
 */
@Slf4j
public class Test4 {
    private static final ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        //创建新的条件变量
        final Condition condition1 = lock.newCondition();
        Runnable runnable = () -> {
            lock.lock();
            try {
                //进入休息室等待
                log.info("准备等待");
                condition1.await();
                log.info("被唤醒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
        Sleeper.sleep(5);
        lock.lock();
        condition1.signal();
        lock.unlock();
    }


}
